<?php
namespace Unicom\Controller;
use Think\Controller;

class ProvinceController extends AdminController {

	// tag  0 上行  1 成功数  2  同步数
	public function search($servcode, $mocontent, $date, $tag) {
		layout(false);
		// var_dump($servcode, $date, $tag);
		// return;
		$model = M('cp.privince', 'cp_');
		switch ($tag) {
		case '0':
            $sql = "select *,phone as mophone  from (select l.id,l.create_time,l.phone,l.servcode as mocontent from cp.cp_order_log as l  where   DATE_FORMAT(l.create_time,'%Y-%m-%d')='{$date}' and l.servcode='{$servcode}' )  as v left join cp.cp_privence as p on left (v.phone,7)=p.dcmobile  order by create_time desc";

            break;
		case '1':
			$sql = "select *,phone as mophone  from (select l.id,l.create_time,l.phone,l.servcode as mocontent from cp.cp_order_log as l  where l.status=0 and  DATE_FORMAT(l.create_time,'%Y-%m-%d')='{$date}' and l.servcode='{$servcode}' )  as v left join cp.cp_privence as p on left (v.phone,7)=p.dcmobile order by create_time desc";

			break;
		case '2':
			$sql = "select *,phone as mophone  from (select l.id,l.create_time,l.phone,l.servcode as mocontent from cp.cp_order_log as l  where  DATE_FORMAT(l.create_time,'%Y-%m-%d')='{$date}' and l.servcode='{$servcode}' and l.sync=0)  as v left join cp.cp_privence as p on left (v.phone,7)=p.dcmobile order by create_time desc";

			break;

		default:

			break;
		}

		$data = $model->query($sql);

		$this->assign('data', $data);
		$this->assign('servcode', $servcode);
		$this->assign('date', $date);
		$this->assign('tag', $tag);
		$this->display();
		//var_dump($data);
	}

	/**
	 * 每条指令的分省情况
	 * @param  [type] $servcode [description]
	 * @param  [type] $date     [description]
	 * @return [type]           [description]
	 */
	public function statByProvince($servcode, $date, $tag) {
		$model = M('cp.privince', 'cp_');
		switch ($tag) {
		case '0':
			$sql = "select *,count(*) as count from cp.cp_verify_phone as v left join cp.cp_privence as p on left (v.mophone,7)=p.dcmobile where v.mocontent='{$servcode}'
						and DATE_FORMAT(v.create_time,'%Y-%m-%d')='{$date}' group by p.dcprivence";
			break;
		case '1':
			$sql = "select *,count(*) as count  from (select l.id,l.create_time,l.phone,v.mocontent from cp.cp_order_log as l left join cp.cp_verify_phone as v on l.phone=v.mophone  where DATE_FORMAT(l.create_time,'%Y-%m-%d')='{$date}' and v.mocontent='{$servcode}' )  as v left join cp.cp_privence as p on left (v.phone,7)=p.dcmobile group by p.dcprivence";

			break;
		case '2':
			$sql = "select *,count(*) as count  from (select l.id,l.create_time,l.phone,v.mocontent from cp.cp_order_log as l left join cp.cp_verify_phone as v on l.phone=v.mophone  where DATE_FORMAT(l.create_time,'%Y-%m-%d')='{$date}' and v.mocontent='{$servcode}' and l.sync=0)  as v left join cp.cp_privence as p on left (v.phone,7)=p.dcmobile group by p.dcprivence";

			break;
		default:
			# code...
			break;
		}
		$data = $model->query($sql); //按省份统计
		$this->ajaxReturn($data);

	}

	/**
	 * 每条指令的分省情况
	 * @param  [type] $servcode [description]
	 * @param  [type] $date     [description]
	 * @return [type]           [description]
	 */
	public function statByProvinceBetween($start_date, $end_date) {

		$model = M('cp.privince', 'cp_');
		$sql = "select *,count(*) as count from (SELECT * from cp_payment_log as l  where DATE_FORMAT(l.create_time,'%Y-%m-%d') BETWEEN '{$start_date}' and '{$end_date}' and l.msgtype=3 and l.status=0) as o left join cp.cp_privence as p on left (o.phone,7)=p.dcmobile group by p.dcprivence;";
		$data = $model->query($sql); //按省份统计
		$this->ajaxReturn($data);

	}

	/**
	 * 每条指令的分省情况三表
	 * @param  [type] $servcode [description]
	 * @param  [type] $date     [description]
	 * @return [type]           [description]
	 */
	public function statByProvinceBetweenT($start_date, $end_date) {
		// $tmp = array(
		// 	'内蒙古' => 5000,
		// 	'黑龙江' => 3000,
		// 	'江苏' => 3000,
		// 	'福建' => 3000,
		// 	'江西' => 3000,
		// 	'湖北' => 2000,
		// 	'湖南' => 2000,
		// 	'广东' => 5000,
		// 	'贵州' => 3000,
		// 	'甘肃' => 3000,
		// 	'青海' => 2000,
		// );

		$model = M('cp.privince', 'cp_');
		$arr = $model->query("select * from cp_limit");
		$tmp = null;
		foreach ($arr as $key => $value) {
			$tmp[$value['privence']] = $value['count'];
		}
		$privence = $model->query("select * from cp_privence group by dcprivence");
		$sql = "select *,count(*) as count from (SELECT * from cp_order_log as l  where DATE_FORMAT(l.create_time,'%Y-%m-%d') BETWEEN '{$start_date}' and '{$end_date}'  and l.status=0) as o left join cp.cp_privence as p on left (o.phone,7)=p.dcmobile group by p.dcprivence;";

		$data = $model->query($sql); //按省份统计

		foreach ($privence as $key => &$value) {
			foreach ($data as $key1 => $value1) {
				if ($value['dcprivence'] == $value1['dcprivence']) {
					$value = $value1;
				}
			}
			if ($value['count']) {

			} else {
				//echo "____";
				$value['count'] = '0';
			}

		}
		foreach ($privence as $key => &$value) {
			$value['max'] = $tmp[$value['dcprivence']];
		}
		//var_dump($privence);
		$this->ajaxReturn($privence);

	}

	/**
	 * 点播，包月的分省情况
	 * @param  [type] $servcode [description]
	 * @param  [type] $date     [description]
	 * @return [type]           [description]
	 */
	public function statMonthByProvince($date) {
		$model = M('cp.privince', 'cp_');
		$sql_by_province = "select *,count(*) as count,
		  case v.mocontent
		      when 'WV' THEN '0'
		      when 'WD' THEN '0'
		      when 'WS' THEN '0'
		      when 'TDWV' THEN '-1'
		      when 'TDWS' THEN '-1'
		      when 'TDWD' THEN '-1'

		      ELSE '1'
		   END as other
		  from cp.cp_verify_phone  as v left join cp.cp_privence as p on left (v.mophone,7)=p.dcmobile where DATE_FORMAT(v.create_time,'%Y-%m-%d')='" . $date . "' group by other";
		$data = $model->query($sql_by_province); //按省份统计
		$this->ajaxReturn($data);
		//var_dump($data);
	}

	public function setLimit($privence, $limit) {
		$model = M('cp.limit', 'cp_');
		if ($model->where(array('privence' => $privence))->find()) {
			$model->where(array('privence' => $privence))->setField('count', $limit);
		} else {
			$model->add(array('privence' => $privence, 'count' => $limit));
		}
		$ret['code'] = 1;

		$this->ajaxReturn($ret);

	}
}